#include <assert.h>
#include <stdlib.h>
#include "pd_define.h"
#include "pd_log.h"
#include "pd_socket.h"
#include "pd_transport.h"
#include "pd_accepter.h"
#include "pd_udp_ioc.h"

int main(int argc, char ** argv)
{
    if (2 > argc)
    {
        PD_LOG(WARN, "Server port must be set\b");
        exit(-1);
    }
    int port = atoi(argv[1]);

    PD_LOG(INFO, "mock_tcp_server main");

    PdSocket *listen_sock = pd_socket_init_tcp_server(port);
    assert(NULL != listen_sock);

    PdSocket *udp_sock = pd_socket_init_udp_server(port + 1);
    assert(NULL != udp_sock);

    PdTransport *ts = pd_transport_init();
    assert(NULL != ts);

    PdIOComponent listen_ioc;
    listen_ioc.sock = listen_sock;
    listen_ioc.ts = ts;
    listen_ioc.on_readable = pd_listenfd_on_readable;
    listen_ioc.on_writeable = pd_listenfd_on_writeable;
    listen_ioc.on_error = pd_listenfd_on_error;

    PdIOComponent udp_ioc;
    udp_ioc.sock = udp_sock;
    udp_ioc.ts = ts;
    udp_ioc.on_readable = pd_udpfd_on_readable;
    udp_ioc.on_writeable = pd_udpfd_on_writeable;
    udp_ioc.on_error = pd_udpfd_on_error;

    int ret = pd_transport_add_ioc(ts, &listen_ioc, 1, 0);
    assert(0 == ret);

    ret = pd_transport_add_ioc(ts, &udp_ioc, 1, 0);
    assert(0 == ret);

    ret = pd_transport_run(ts);
    assert(0 == ret);

    pd_transport_wait(ts);

    return 0;
}

